import React from 'react';
import { Button, Modal, message } from 'antd';
import { inject, observer } from 'mobx-react';
import { WeaTop, WeaTab, WeaLeftRightLayout, WeaRightMenu, WeaPopoverHrm, WeaTools, WeaErrorPage, WeaDialog } from 'ecCom';
import { observable, action, toJS } from 'mobx';
import { WeaTableNew } from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import CptInventoryPlanForm from '../form/CptInventoryPlanForm';

@inject("cptInventoryStore")
@observer
class PlanMainDialog extends React.Component {
    constructor(props) {
        super(props);
    }
    doInit(props) {
        const { cptInventoryStore } = props;
        const { doPlanMainSearch } = cptInventoryStore;
        doPlanMainSearch();// 查找盘点总表    
    }
    componentDidMount() {
        this.doInit(this.props);
    }

    shouldComponentUpdate(nextProps, nextState) {
        return true;
    }
    componentWillUnmount() {
        const { cptInventoryStore } = this.props;
        cptInventoryStore.clearStatus();
    }

    render() {
        const { cptInventoryStore } = this.props;
        const { planmaindataKey, planmaintableStore, inventoryform, inventoryformCondition, inventoryVisible,setInventoryVisible } = cptInventoryStore;
        let editButtons = [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@jq4tlj@close`} type="ghost" onClick={() => setInventoryVisible(false) } >{getLabel(309, "关闭")}</Button>)
        ]
        let dialogHeight = window.innerHeight;
        if (dialogHeight > 768) {
            dialogHeight = 600;
        }else{
            dialogHeight = 500;
        }

        return (
            <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@gijoz3`} datas={this.getRightMenu()} >
                <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@1v4ukb`}
                    sessionkey={planmaindataKey}
                    comsWeaTableStore={planmaintableStore}
                    hasOrder={true}
                    needScroll={true}
                    outerHeight={dialogHeight-60}
                    onOperatesClick={this.onOperatesClick}
                />
                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@iml3uu`}
                    title={getLabel(1506, "盘点") + getLabel(535, "资产")}
                    visible={inventoryVisible}
                    onCancel={() => { setInventoryVisible(false) }}
                    icon='icon-coms-fa'
                    iconBgcolor='#f14a2d'
                    buttons={editButtons}
                >
                    <CptInventoryPlanForm ecId={`${this && this.props && this.props.ecId || ''}_CptInventoryPlanForm@l8o646`} form={inventoryform} formcondition={inventoryformCondition} />
                </WeaDialog>
            </WeaRightMenu>
        )
    }

    onOperatesClick = (record, index, operate, flag, argumentString) => {
        const { cptInventoryStore } = this.props;
        const { doDeleteMainCpt } = cptInventoryStore;
        let _href = operate && operate.href ? operate.href : "";
        let fn = _href.replace("javascript:", "");
        fn = fn.substring(0, fn.indexOf('('));
        let that = this;
        if (fn != "") {
            if ("onDel" == fn) {
                Modal.confirm({
                    title: getLabel(15172, '系统提示'),
                    content: getLabel(83600, '您确认要删除吗?'),
                    onOk() {
                        doDeleteMainCpt(record.randomFieldId);
                    },
                    onCancel() { },
                });
            }else if ("viewInventory" == fn) {
                cptInventoryStore.getInventoryForm({ inventoryId: record.id,viewAttr:1 });
                cptInventoryStore.setInventoryVisible(true);
            }
        }
    }

    getRightMenu() {
        const { cptInventoryStore } = this.props;
        const { planmaintableStore, exportMain, canEditlist, canExcelOut } = cptInventoryStore;
        const selectedLen = planmaintableStore.selectedRowKeys.length;
        let btns = [];
        if (canEditlist) {
            btns.push({
                key: "batchDelete",
                disabled: selectedLen == 0,
                icon: <i className='icon-coms-delete' />,
                content: getLabel(32136, '批量删除'),
                onClick: key => {
                    this.batchDeleteClick();
                }
            });
        }
        if (!canExcelOut) {
            btns.push({
                key: "exportMain",
                icon: <i className='icon-coms-Preservation' />,
                content: getLabel(28343, '导出Excel'),
                onClick: key => {
                    exportMain();
                }
            });
        }

        return btns;
    }
    batchDeleteClick = () => {//批量删除
        const { planmaintableStore } = this.props.contentStore;
        const { selectedRowKeys } = planmaintableStore;//得到选中数据的id
        const count = toJS(selectedRowKeys).length;
        let contentStore = this.props.contentStore;
        Modal.confirm({
            title: getLabel(15172, '系统提示'),
            content: getLabel(83600, '您确认要删除吗?'),
            onOk() {
                contentStore.doDeleteMainCpt(selectedRowKeys);
            },
            onCancel() { },
        });
    }
}

export default WeaTools.tryCatch(React,
    props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@s8zz0n`} msg={props.error ? props.error : getLabel(383324, '对不起，该页面异常，请联系管理员')} />,
    { error: "" }
)(PlanMainDialog);